import {
  getCategory1Api,
  getCategory2Api,
  getCategory3Api,
} from "@/api/product/category";
import { defineStore } from "pinia";
import type { categoryIdsListsModel } from "./interface";

/**

 */
export const useCategoryStore = defineStore("category", {
  state: (): categoryIdsListsModel => ({
    category1Id: null,
    category2Id: null,
    category3Id: null,
    category1List: [],
    category2List: [],
    category3List: [],
  }),

  actions: {
    async getCategory1List() {
      this.category1List = await getCategory1Api();
    },
    async getCategory2List(category1Id: number) {
      this.category1Id = category1Id;
      this.category2List = await getCategory2Api(category1Id);
      this.category2Id = null;
      this.category3Id = null;
      this.category3List = [];
    },
    async getCategory3List(category2Id: number) {
      this.category2Id = category2Id;
      this.category3List = await getCategory3Api(category2Id);
      this.category3Id = null;
    },

    // 重置状态
    reset() {
      this.category1Id = undefined!;
      this.category2Id = undefined!;
      this.category3Id = undefined!;
      this.category1List = [];
      this.category2List = [];
      this.category3List = [];
    },
  },
});
